package com.iyjhsoc.game.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iyjhsoc.game.dao.DictDAO;
import com.iyjhsoc.game.dao.DictItemDAO;
import com.iyjhsoc.game.domain.entity.DictDO;
import com.iyjhsoc.game.domain.entity.DictItemDO;
import com.iyjhsoc.game.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.DataClassRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @Author xie
 * @date 2024/9/5
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictDAO, DictDO> implements DictService {

    @Autowired
    DictDAO dictDAO;
    @Autowired
    DictItemDAO dictItemDAO;

    @Override
    public List<DictItemDO> getDictInfo(String dictType) {
        QueryWrapper<DictDO> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("dict_type",dictType);
        DictDO dictDO1 = dictDAO.selectOne(queryWrapper);
        if("1".equals(dictDO1.getType())){
            RowMapper<DictItemDO> rowMapper = new DataClassRowMapper();
            List<DictItemDO> query = dictItemDAO.querySql(dictDO1.getDictSql());
            return query;
        } else {

        }
        return null;
    }

    @Override
    public Map<String, List<DictItemDO>> getDictInfoByDictTypes(String dictType) {
        QueryWrapper<DictDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dict_type", Arrays.asList(dictType.split(",")));
        List<DictDO> dictDOS = dictDAO.selectList(queryWrapper);
        Map<String,List<DictItemDO>> res = new HashMap<>();
        for(DictDO dictDO: dictDOS){
            if("1".equals(dictDO.getType())){
                RowMapper<DictItemDO> rowMapper = new DataClassRowMapper();
                if(StringUtils.isBlank(dictDO.getDictSql())){
                    continue;
                }
                List<DictItemDO> query = dictItemDAO.querySql(dictDO.getDictSql());
                res.put(dictDO.getDictType(),query);
            } else if("2".equals(dictDO.getType())) {
                List<DictItemDO> dictItemsByDictType = dictItemDAO.getDictItemsByDictType(dictDO.getDictType());
                res.put(dictDO.getDictType(),dictItemsByDictType);
            }
        }
        return res;
    }

    @Override
    public Map<String, List<DictItemDO>> getDictInfosWhere(String dictType, String where) {
        QueryWrapper<DictDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dict_type", dictType);
        DictDO dictDO = dictDAO.selectOne(queryWrapper);
        Map<String,List<DictItemDO>> res = new HashMap<>();
        if("1".equals(dictDO.getType())){
            RowMapper<DictItemDO> rowMapper = new DataClassRowMapper();
            if(StringUtils.isBlank(dictDO.getDictSql())){
                throw new RuntimeException("数据异常");
            }
            String sql = dictDO.getDictSql() + " where " + where;
            List<DictItemDO> query = dictItemDAO.querySql(sql);
            res.put(dictDO.getDictType(),query);
        } else if("2".equals(dictDO.getType())) {
            throw  new RuntimeException("暂不支持码值");
        }
        return res;
    }
}
